package com.joysuch.wwyt.datadump.biz.convertor;


import cn.zlg.common.datadump.annotation.DataDumpField;
import cn.zlg.common.datadump.convertor.FieldValueConvert;
import com.google.common.base.Splitter;
import com.joysuch.wwyt.common.constant.Constant;
import com.joysuch.wwyt.core.repository.BaseUserDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component
public class ImportMhMajorHazardUserConvertor implements FieldValueConvert<String, String> {

    @Autowired
    private BaseUserDao baseUserDao;

    @Override
    public String convert(Object bean, String value, DataDumpField dataField) {
        String nameStr = "";
        if (StringUtils.isNotBlank(value)) {
            List<String> userName = Splitter.on(Constant.SPLIT_COMMA).trimResults().splitToList(value);
            nameStr = baseUserDao.getIdByNames(userName).stream().map(e->String.valueOf(e)).collect(Collectors.joining(Constant.SPLIT_COMMA));
            if(StringUtils.isBlank(nameStr)){
                throw new IllegalArgumentException("请填写正确的人员名称");
            }
        }
        return nameStr;
    }
}
